package br.usp.ime.protoc.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.gwtext.client.widgets.menu.event.MenuListenerAdapter;

public class MainView extends Composite {
	private Panel root = new Panel();
	private Panel main = new Panel();
	private Panel contents = new Panel();
	Toolbar toolbar = new Toolbar();

	public static int HEIGHT=600;
	//public int MAIN_VIEW_HEIGHT=600;

	public class MyMenuItem extends Item {
		//private final Maker makeInst;
		
		public MyMenuItem(String text,final Maker makeInst) {
			this.setTitle(text);
			this.setText(text);
			this.addListener(new BaseItemListenerAdapter() {
				@Override
				public void onClick(BaseItem item, EventObject e) {
					//Class klass=((MyMenuItem)item).klass;
					//GWT.log(klass.toString(),null);
					contents.clear();
					contents.add((Widget)makeInst.make());
					contents.doLayout();
					super.onClick(item, e);
				}
			});
		}
	};
	
	private Menu makeMenu(Item[] items) {
		Menu menu = new Menu();
		menu.setShadow(true);
		menu.setMinWidth(10);
		for (Item item : items) {
			menu.addItem(item);
		}
		return menu;
	}

	private ToolbarButton makeMenuButton(String name, Item[] items) {
		Menu menu = makeMenu(items);
		menu.addListener(new MenuListenerAdapter() {
			@Override
			public void onItemClick(BaseItem item, EventObject e) {
				GWT.log(item.getTitle() + " clicked", null);
				//contents.setHtml(item.getTitle());
				super.onItemClick(item, e);
			}
		});
		ToolbarButton menuButton = new ToolbarButton(name);
		menuButton.setMenu(menu);
		return menuButton;
	}

	public MainView() {
		root.setBorder(false);
		root.setPaddings(15);
		root.setLayout(new FitLayout());
		// root.add(main);
		main.setLayout(new BorderLayout());

		toolbar.addButton(makeMenuButton("Pacientes", 
				new Item[] {new MyMenuItem("Todos",new Maker() {
					public Object make() {
						return GWT.create(PacientListView.class);
					}
				}),
				new MyMenuItem("Test",new Maker() {
					public Object make() {
						return GWT.create(PacienteTestView.class);
					}
				}),		
				new MyMenuItem("Parentesco",new Maker() {
					public Object make() {
						return GWT.create(ParentescoView.class);
					}
				})		
		}));

		//toolbar.addButton(makeMenuButton("Cadastros", 
		//		new String[] { "Cidades","Coisas", "Stuff" }));

		toolbar.addSeparator();

		toolbar.addButton(makeMenuButton("Ajuda", 
				new Item[] {new MyMenuItem("Ajuda",new Maker() {
					public Object make() {
						return GWT.create(HelpView.class);		
					}
				}),
				new MyMenuItem("Sobre",new Maker() {
					public Object make() {
						return GWT.create(AboutView.class);		
					}
				})
			}));

		
		// contents.setTitle("Center");
		contents.setWidth("100%");
		contents.setWidth(HEIGHT);
		contents.add(new Label("Click no menu"));
		main.add(toolbar, new BorderLayoutData(RegionPosition.NORTH));
		main.add(contents, new BorderLayoutData(RegionPosition.CENTER));
		root.add(main);
		// root.add(toolbar);
		// root.add(contents);
		Viewport viewport = new Viewport(root);
		initWidget(root);
	}
}
